#include <evolutive/Dna.h>
#include <math.h>
#include <iostream>

Dna::Dna(int genSize, int numGens) : genSize(genSize), numGens(numGens){
	cromossomes = new unsigned char[numGens * genSize];
}

Dna::~Dna() {
	delete cromossomes;
}

void Dna::put(int value, int pos){

	if(value > pow(2,genSize)){
		std::cerr << "Value:" << value << "\n";
		throw "Value cannot be included in a gen";
	}

	if(pos >= numGens) {
		std::cerr << "Pos:" << pos << "\n";
		throw "Invalid gen position";
	}

	int genStart = pos*genSize;

	int nValue = value;

	for(int i = genSize-1; i >= 0; i--) {
		this->cromossomes[genStart+i] = nValue%2;
		nValue = nValue/2;
	}
}

int Dna::get(int pos){

	if(pos >= numGens) {
		throw "There is not gen in position";
	}

	int value = 0;

	int genStart = pos*genSize;

	for(int i = genSize-1; i >= 0; i--) {
		value += this->cromossomes[genStart+i] * pow(2, 2-i);
	}

	return value;
}

